rm(list=ls())

#set working directory that includes this script and the raw data files

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#check working directory is correct
getwd()

#Load all the packages needed for analysis

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyr, dplyr, lmerTest, stargazer, 
               mfx, ggplot2, ggpubr, reshape2
)
  

#Read data from working directory
df <- read.csv(file = "RawData/Evasion study Sender Final_September 17, 2019_14.42.csv", header = T, sep = ",")

#remove additional headers generated by Qualtrics
df <- df %>%  filter(!row_number() %in% c(1, 2))

n <- nrow(df) # N = 1211

df<-df%>%
  dplyr::rename(Prolific_id=Q37, Duration=Duration..in.seconds.,
                Control_q1=Q12,	errors_q1=Q13,	Control_q2=Q14,	errors_q2=Q15,	
                Control_q3=Q16,	errors_q3=Q17,	Control_q4=Q18,	errors_q4=Q19,	
                Control_q5=Q20,	errors_q5=Q21, 
                decision=Q24,	belief_1=Q30,	belief_2=Q39, belief_3=Q31,
                gender=Q33,	gender_other=Q33_3_TEXT, age=Q34_1_TEXT, 
                education=Q46, education_other=Q46_7_TEXT,
                condition=Name)%>%
  dplyr::mutate(sub_id=row_number())


# Check if any duplicates
summary(duplicated(df$Prolific_id)) # TRUE = 0


# Summary stats to see how many people didn't finish the survey
table(df$Finished) # FALSE = 0 
df <- subset(df, df$Finished == 1) 
# N = 1211


#------------------------------
# CREATE AND EDIT VARS
#------------------------------

evasion_S1<-dplyr::select(df, Duration, ResponseId, Control_q1,	errors_q1,	Control_q2,	errors_q2,	Control_q3,	errors_q3,	Control_q4,	errors_q4,	Control_q5,	errors_q5,
                       decision,	belief_1,	belief_2, belief_3, gender, gender_other, age, 
                       education, education_other, condition, 	Prolific_id,	Spinner_Outcome,	X,	Z, sub_id)
evasion_S1$belief_1<-as.numeric(evasion_S1$belief_1)
evasion_S1$belief_2<-as.numeric(evasion_S1$belief_2)
evasion_S1$belief_3<-as.numeric(evasion_S1$belief_3)
evasion_S1$decision<-as.numeric(evasion_S1$decision)
evasion_S1$gender<-as.numeric(evasion_S1$gender)
evasion_S1$education<-as.numeric(evasion_S1$education)

evasion_S1<-evasion_S1 %>%
  mutate(treat=ifelse(condition=="Treatment I don't know", "t_idk",
                      ifelse(condition=="Treatment silence", "t_s",
                             ifelse(condition=="Treatment Half-truth", "t_ht",
                                    ifelse(condition=="Control silence", "c_s",
                                           ifelse(condition=="Control Half-truth", "c_ht","c_idk"))))),
         decision_code=ifelse(decision==1,"truth","lie"))%>% 
  drop_na(decision)

evasion_S1<-evasion_S1 %>%
  mutate(treat_pool=ifelse(treat=="c_s"|treat=="c_ht"|treat=="c_idk","control",treat),
         lie=ifelse(decision_code=="lie",1,0))

evasion_S1<-arrange(evasion_S1, condition)



# Drop unused factor levels from dataset
evasion_S1 <- droplevels(evasion_S1)



evasion_S1$age<-as.character(evasion_S1$age)

df_S1<-evasion_S1%>%
  mutate(age_clean=ifelse(age=="-99"| age=="2p", NA,
                          ifelse(age=="32 years", "32",
                                 ifelse(age=="fifty", "50",
                                        ifelse(age=="Fifty four", "54",
                                               ifelse(age=="fifty one", "51", age))))),
         female=ifelse(gender==1,1,
                       ifelse(gender==4, NA, 0)))%>%
  mutate(educ_recode = ifelse(education>=10,"high",
                              ifelse(education==9 | education ==1, "medium_low", 
                                     ifelse(education==7, "other",NA))))%>%
  mutate(educ_high=ifelse(educ_recode=="high",1,0))%>%
  dplyr::rename(b_red_after_X=belief_1, b_red_after_BLUE=belief_2, b_liars=belief_3)%>%
  mutate(treat_pool_bin=ifelse(treat_pool=="control", "direct_lie", "evasion"),
         experiment="hidden")

df_S1$age_clean<-as.numeric(df_S1$age_clean)




# -------- Write output data to file ## ----------

# R format
#filename <- paste("Data-Evasion-S1", Sys.Date(), ".Rdata", sep="")
filename <- "Data-Evasion-S1.Rdata"
save(df_S1, file = filename)
# .csv format
write.csv(df_S1, "Data-Evasion-S1.csv")



